package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.AbandonStudent;
import cn.wolfcode.crm.domain.FormalStudent;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IFormalStudentService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("formalStudent")
public class FormalStudentController {
    @Autowired
    public IFormalStudentService formalStudentService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"formalStudent:view","正式学员列表"},logical = Logical.OR)
    public String view() {
        return "formalStudent/formalStudent";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(QueryObject qo) {

        return formalStudentService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<FormalStudent> selectAll() {
        return formalStudentService.selectAll();
    }

    @RequestMapping("selectAllPayed")
    @ResponseBody
    public List<FormalStudent> selectAllPayed() {
        return formalStudentService.selectAllPayed();
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"formalStudent:delete","正式学员删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            formalStudentService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }
    @RequestMapping("transfer")
    @RequiresPermissions(value = {"formalStudent:delete","正式学员删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult transfer(Long targetClassId,Long id) {
        try {
            formalStudentService.transfer(targetClassId,id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("转班失败");
        }
    }

    /**
     * 学员流失功能
     * @param targetClassId
     * @param id
     * @return
     */
    @RequestMapping("bandon")
    @RequiresPermissions(value = {"formalStudent:delete","正式学员删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult bandon(Long formalStudentId, AbandonStudent abandonStudent) {
        try {
            formalStudentService.bandon(formalStudentId,abandonStudent);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("转班失败");
        }
    }
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"formalStudent:saveOrUpdate","正式学员新增或编辑"},logical = Logical.OR)
    public JSONResult saveOrUpdate(FormalStudent formalStudent){
        try {
            if(formalStudent.getId() != null){
                formalStudentService.updateByPrimaryKey(formalStudent);
            }else{
                formalStudentService.insert(formalStudent);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
}
